//
//  ZZAudioTool.m
//  planning
//
//  Created by mac on 2019/7/30.
//  Copyright © 2019 mac. All rights reserved.
//

#import "ZZAudioTool.h"

#import "ZZMusic.h"

#import <MediaPlayer/MediaPlayer.h>

@implementation ZZAudioTool

/**
 
 *  存放所有的音频ID
 
 *  字典: filename作为key, soundID作为value
 
 */

static  NSMutableDictionary *_soundIDDict;

/**
 
 *  存放所有的音乐播放器
 
 *  字典: filename作为key, audioPlayer作为value
 
 */

static NSMutableDictionary *_audioPlayerDict;

/**
 
 *  返回请求单例对象
 
 */

+ (instancetype)shareInstance

{
    
    static ZZAudioTool *audioTool;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        if (audioTool == nil) {
            
            audioTool = [[ZZAudioTool alloc] init];
            
        }
        
    });
    
    return audioTool;
    
}

- (void)prepareToPlayWithMusic:(ZZMusic *)music

{
    
    if (!music.filename) return;
    
    // 创建播放器
    
    NSURL *musicURL = [NSURL fileURLWithPath:music.filename];
    
    self.player = [[AVAudioPlayer  alloc] initWithContentsOfURL:musicURL error:nil];
    
    // 准备
    [self.player  prepareToPlay];
    
    // 设置锁屏歌曲信息
    [self  setUpLockInfoWithMP3Info:music];
    
}

- (void)setUpLockInfoWithMP3Info:(ZZMusic *)music

{
    
    NSLog(@"%s",__func__);
    
    //锁屏信息设置
    NSMutableDictionary *playingInfo = [NSMutableDictionary  dictionary];
    
    //1.专辑名称
    playingInfo[MPMediaItemPropertyAlbumTitle] = @"中文十大金曲";
    
    //2.歌曲
    playingInfo[MPMediaItemPropertyTitle] = music.name;
    
    //3.歌手名称
    playingInfo[MPMediaItemPropertyTitle] = music.singer;
    
    //4.专辑图片
    if(music.icon){
        
        UIImage *artWorkImage = [UIImage imageNamed:music.icon];
        
        MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:artWorkImage];
        
        playingInfo[MPMediaItemPropertyArtwork] = artwork;
        
    }
    
    //5.锁屏音乐总时间
    
    playingInfo[MPMediaItemPropertyPlaybackDuration] = @(self.player.duration);
    
    //设置锁屏时的播放信息
    
    [MPNowPlayingInfoCenter  defaultCenter].nowPlayingInfo = playingInfo;
    
}

/*
 
 * 播放
 
 */

- (void)play

{
    
    [self.player play];
    
}

/*
 
 * 暂停
 
 */

- (void)pause

{
    
    [self.player pause];
    
}

/**
 
 *  初始化
 
 */

+ (void)initialize

{
    
    _soundIDDict = [NSMutableDictionary  dictionary];
    
    _audioPlayerDict = [NSMutableDictionary  dictionary];
    
}

/**
 
 *  播放音效
 
 *
 
 *  @param filename 音效文件名
 
 */

+ (void)playSound:(NSString *)filename

{
    
    if (!filename) return;
    
    // 1.从字典中取出soundID
    
    SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];
    
    if (!soundID) { // 创建
        
        // 加载音效文件
        
        NSURL *url = [[NSBundle  mainBundle] URLForResource:filename withExtension:nil];
        
        if (!url) return;
        
        // 创建音效ID
        
        AudioServicesCreateSystemSoundID((__bridge  CFURLRef)url, &soundID);
        
        // 放入字典
        
        _soundIDDict[filename] = @(soundID);
        
    }
    
    // 2.播放
    
    AudioServicesPlaySystemSound(soundID);
    
}

/**
 
 *  销毁音效
 
 *
 
 *  @param filename 音效文件名
 
 */

+ (void)disposeSound:(NSString *)filename

{
    
    if (!filename) return;
    
    SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];
    
    if (soundID) {
        
        // 销毁音效ID
        
        AudioServicesDisposeSystemSoundID(soundID);
        
        // 从字典中移除
        
        [_soundIDDict removeObjectForKey:filename];
        
    }
    
}

/**
 
 *  播放音乐
 
 *
 
 *  @param filename 音乐文件名
 
 */

+ (AVAudioPlayer *)playMusic:(NSString *)filename

{
    
    if (!filename) return nil;
    
    // 1.从字典中取出audioPlayer
    
    AVAudioPlayer *audioPlayer = _audioPlayerDict[filename];
    
    if (!audioPlayer) { // 创建
        
        // 加载音乐文件
        
        NSURL *url = [[NSBundle  mainBundle] URLForResource:filename withExtension:nil];
        
        if (!url) return nil;
        
        // 创建audioPlayer
        
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        
        // 缓冲
        
        [audioPlayer prepareToPlay];
        
        //        audioPlayer.enableRate = YES;
        
        //        audioPlayer.rate = 10.0;
        
        // 放入字典
        
        _audioPlayerDict[filename] = audioPlayer;
        
    }
    
    // 2.播放
    
    if (!audioPlayer.isPlaying) {
        
        [audioPlayer play];
        
    }
    
    return audioPlayer;
    
}

/**
 
 *  暂停音乐
 
 *
 
 *  @param filename 音乐文件名
 
 */

+ (void)pauseMusic:(NSString *)filename

{
    
    if (!filename) return;
    
    // 1.从字典中取出audioPlayer
    
    AVAudioPlayer *audioPlayer = _audioPlayerDict[filename];
    
    // 2.暂停
    
    if (audioPlayer.isPlaying) {
        
        [audioPlayer pause];
        
    }
    
}

/**
 
 *  停止音乐
 
 *
 
 *  @param filename 音乐文件名
 
 */

+ (void)stopMusic:(NSString *)filename

{
    
    if (!filename) return;
    
    // 1.从字典中取出audioPlayer
    
    AVAudioPlayer *audioPlayer = _audioPlayerDict[filename];
    
    // 2.暂停
    
    if (audioPlayer.isPlaying) {
        
        [audioPlayer stop];
        
        // 直接销毁
        
        [_audioPlayerDict removeObjectForKey:filename];
        
    }
    
}

/**
 
 *  返回当前正在播放的音乐播放器
 
 */

+ (AVAudioPlayer *)currentPlayingAudioPlayer

{
    
    for (NSString *filename in _audioPlayerDict) {
        
        AVAudioPlayer *audioPlayer = _audioPlayerDict[filename];
        
        if (audioPlayer.isPlaying) {
            
            return audioPlayer;
            
        }
        
    }
    
    return  nil;
    
}

@end
